package me.zhengjie.modules.web.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import me.zhengjie.constant.ConstantUtils;
import me.zhengjie.modules.system.service.AtmrService;
import me.zhengjie.modules.system.service.dto.AtmrDto;
import me.zhengjie.modules.system.service.dto.AtmrQueryCriteria;
import me.zhengjie.modules.web.domain.course.CourseIndexResp;
import me.zhengjie.modules.web.domain.index.AtmrIndexResp;
import me.zhengjie.modules.web.domain.index.AtmrListResp;
import me.zhengjie.modules.web.domain.index.IndexResp;
import me.zhengjie.modules.web.service.IIndexService;
import me.zhengjie.modules.web.service.course.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class IndexServiceImpl implements IIndexService {
    @Autowired
    private ICourseService courseService;

    @Autowired
    private AtmrService atmrService;
    @Override
    public IndexResp index() {

        IndexResp resp = new IndexResp();
        List<CourseIndexResp> list = courseService.courseIndex();
        resp.setCourseList(list);
        AtmrQueryCriteria atmrQueryCriteria = new AtmrQueryCriteria();
        atmrQueryCriteria.setStatus(String.valueOf(ConstantUtils.InValid));
        List<AtmrDto> atmrDtos = atmrService.queryAll(atmrQueryCriteria);

        List<AtmrIndexResp> atmrIndexResps = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(atmrDtos)){
            atmrDtos.stream().limit(4).forEach(s -> {
                AtmrIndexResp indexResp = new AtmrIndexResp();
                indexResp.setName(s.getAtmrName());
                indexResp.setPic(s.getIconUrl());
                indexResp.setPrice(new BigDecimal(s.getPrice()));
                indexResp.setAtmrId(s.getId());
                indexResp.setAgeType(s.getAgeType());
                indexResp.setAtmrType(s.getAtmrType());
                atmrIndexResps.add(indexResp);
            });
        }
        resp.setAtmrList(atmrIndexResps);
        return resp;
    }


    @Override
    public List<AtmrListResp> list() {
        List<AtmrListResp> respList = new ArrayList<>();
        AtmrQueryCriteria atmrQueryCriteria = new AtmrQueryCriteria();
        atmrQueryCriteria.setStatus(String.valueOf(ConstantUtils.InValid));
        List<AtmrDto> atmrDtos = atmrService.queryAll(atmrQueryCriteria);
        if (CollectionUtil.isEmpty(atmrDtos)){
            return respList;
        }
        AtmrListResp resp = new AtmrListResp();
        resp.setTypeName("心智测评");
        resp.setTypeId(1);
        List<AtmrIndexResp> atmrIndexResps = new ArrayList<>();
        atmrDtos.stream().forEach(s -> {
            AtmrIndexResp indexResp = new AtmrIndexResp();
            indexResp.setAtmrId(s.getId());
            indexResp.setPic(s.getIconUrl());
            indexResp.setName(s.getAtmrName());
            indexResp.setPrice(new BigDecimal(s.getPrice()));
            atmrIndexResps.add(indexResp);
        });
        resp.setAtmrList(atmrIndexResps);
        respList.add(resp);
        return respList;
    }
}
